<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 你
  Date: 2020/10/9
  Time: 10:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
  request.setCharacterEncoding("utf-8");
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  //加载驱动
  Class.forName("com.mysql.jdbc.Driver");
  //建立连接
  String url="jbdc:mysql://localhost:3306/book";
  Connection connection = DriverManager.getConnection(url,"root","root");
  //sql 语句模板
  String sql= "select * from user where username = ? and password = ?";
  //使用PreparedStatement对象执行SQL语句
  PreparedStatement ps =connection.prepareStatement(sql);

  ps.setString(1,username);
  ps.setString(2,password);
  //执行查询
  ResultSet rs = ps.executeQuery();

  if (rs.next()){
    session.setAttribute("username",username);
    response.sendRedirect("home.jsp");
  }else{
    out.print("登陆失败，3秒之后跳转登陆页面...");
    response.setHeader("refresh","3;url=login.jsp");
  }
%>